package pl.com.ozzy.ozsign.tools;

import java.util.HashMap;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * Simple wrapper for shared preferences mechanism.
 * It uses android.content.SharedPreferences to access
 * (read and write) application specific data from XML 
 * file placed in application directory (usually placed 
 * at /data/data/application_package_name/shared_prefs directory).
 * In this case it will be 
 * /data/data/pl.com.ozzy.ozsign.ui/shared_prefs directory.
 */
public class PreferencesWrapper {
	
	private static final String THIS_FILE = "PreferencesWrapper";
	private SharedPreferences prefs;

	
	public PreferencesWrapper(Context appCtx) {
		prefs = PreferenceManager.getDefaultSharedPreferences(appCtx);
	}
	

	private final static HashMap<String, String> STRING_PREFS = new HashMap<String, String>(){
		private static final long serialVersionUID = 1L;
	{
		
	}};
	
	
	private final static HashMap<String, Boolean> BOOLEAN_PREFS = new HashMap<String, Boolean>(){
		private static final long serialVersionUID = 1L;
	{
		put(AppConfigManager.RESET_SETTINGS, true);
		put(AppConfigManager.USE_MEDIA_SCANNER, false);
		put(AppConfigManager.SIGN_IMAGES, false);
		put(AppConfigManager.SIGN_VIDEOS, false);
	}};
	
	
	private final static HashMap<String, Float> FLOAT_PREFS = new HashMap<String, Float>(){
		private static final long serialVersionUID = 1L;
	{

	}};
	
	
	/**
	 * Set a preference string value
	 * @param key the preference key to set
	 * @param value the value for this key
	 */
	public void setStringValue(String key, String value) {
		Editor editor = prefs.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	
	/**
	 * Set a preference boolean value
	 * @param key the preference key to set
	 * @param value the value for this key
	 */
	public void setBooleanValue(String key, boolean value) {
		Editor editor = prefs.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	
	
	/**
	 * Set a preference float value
	 * @param key the preference key to set
	 * @param value the value for this key
	 */
	public void setFloatValue(String key, float value) {
		Editor editor = prefs.edit();
		editor.putFloat(key, value);
		editor.commit();
	}
	
	
	//Private static getters
	// For string
	private static String gPrefStringValue(SharedPreferences aPrefs, String key) {
		if(STRING_PREFS.containsKey(key)) {
			return aPrefs.getString(key, STRING_PREFS.get(key));
		}
		return null;
	}
	
	
	// For boolean
	private static Boolean gPrefBooleanValue(SharedPreferences aPrefs, String key) {
		if(BOOLEAN_PREFS.containsKey(key)) {
			return aPrefs.getBoolean(key, BOOLEAN_PREFS.get(key));
		}
		return null;
	}
	
	// For float
	private static Float gPrefFloatValue(SharedPreferences aPrefs, String key) {
		if(FLOAT_PREFS.containsKey(key)) {
			return aPrefs.getFloat(key, FLOAT_PREFS.get(key));
		}
		return null;
	}
	
	
	/**
	 * Get string preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public String getPreferenceStringValue(String key) {
		return gPrefStringValue(prefs, key);
	}
	
	
	/**
	 * Get boolean preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public Boolean getBooleanValue(String key) {
		return gPrefBooleanValue(prefs, key);
	}
	
	
	/**
	 * Get float preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public Float getPreferenceFloatValue(String key) {
		return gPrefFloatValue(prefs, key);
	}
	
	
	/**
	 * Get integer preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public int getPreferenceIntegerValue(String key) {
		try {
			return Integer.parseInt(getPreferenceStringValue(key));
		}catch(NumberFormatException e) {
			Log.e(THIS_FILE, "Invalid "+key+" format : expect a int");
		}
		return Integer.parseInt(STRING_PREFS.get(key));
	}
	
	
	/**
	 * Set all values to default
	 */
	public void resetToDefaults() {
		for(String key : STRING_PREFS.keySet() ) {
			setStringValue(key, STRING_PREFS.get(key));
		}
		for(String key : BOOLEAN_PREFS.keySet() ) {
			setBooleanValue(key, BOOLEAN_PREFS.get(key));
		}
		for(String key : FLOAT_PREFS.keySet() ) {
			setFloatValue(key, FLOAT_PREFS.get(key));
		}
	}
}
